﻿/*
 * This file is part of quickReminder, a software for gamers
 *
 * Copyright (C) 2006-2009 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.IO;
using LucasCode.Win32;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Collections.Specialized;

namespace LucasCode.Tools
{
    public static class GameInterop
    {
        /*public static string getModFromDir(string path)
        {
            string mod = string.Empty;
            DateTime LastWriteAccess = DateTime.MinValue;
            DirectoryInfo[] dirs = new DirectoryInfo(path).GetDirectories();
            foreach (DirectoryInfo d in dirs)
            {
                FileInfo icon = new FileInfo(Path.Combine(d.FullName, "game.ico"));
                if (icon.Exists)
                {
                    if (icon.LastWriteTime > LastWriteAccess)
                    {
                        mod = d.Name;
                        LastWriteAccess = icon.LastWriteTime;
                    }
                }
            }
            return mod;
        }*/
        
        //public static readonly string userconfigExecCommand = @"(^|;)\s*exec\s+(""?)userconfig.cfg\2\s*(//.*?)?[;\r$]";
        public enum Keys : ushort
        {
            TAB = 0x0F,
            CTRL = 0x1D,
            SHIFT = 0x2A,
            CAPSLOCK = 0x3A,
            F1 = 0x3B,
            F2 = 0x3C,
            F3 = 0x3D,
            F4 = 0x3E,
            F5 = 0x3F,
            F6 = 0x40,
            F7 = 0x41,
            F8 = 0x42,
            F9 = 0x43,
            F10 = 0x44,
            F11 = 0x57,
            F12 = 0x58,
        }

        private static Keys m_InterOpKey = Keys.F10;
        public static Keys InterOpKey
        {
            get { return m_InterOpKey; }
            set { m_InterOpKey = value; }
        }
        private static string m_FullPathToInteropFile = string.Empty;

        public static void ExecHLCommand(string cmd)
        {
            ExecHLCommand(cmd, m_FullPathToInteropFile, IntPtr.Zero);
        }
        private static StringCollection m_CommandQueue = new StringCollection();
        public static void ExecHLCommand(string cmd, string cfgfile)
        {
            ExecHLCommand(cmd, cfgfile, IntPtr.Zero);
        }
        public static void ExecHLCommand(string cmd, string cfgfile, IntPtr winhandle)
        {
            if (string.IsNullOrEmpty(cfgfile))
                return;
            Trace.WriteLine("GameInterop: trying to execute command " + cmd);
            try
            {
                using (StreamWriter w = new StreamWriter(cfgfile, false))
                {
                    w.Write(cmd);
                }
                if (winhandle != IntPtr.Zero)
                    User32.SetForegroundWindow(winhandle);
                DirectInput.SendKeyPress((DirectInput.Keys)m_InterOpKey);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.WriteLine("Error in TeamChatWarnModule: " + ex.ToString());
            }
        }

        public static void CheckInterOpBinding(string gamepath)
        {
            try
            {
                string config = Path.Combine(gamepath, "config.cfg");
                string content = String.Empty;
                using (StreamReader r = new StreamReader(config))
                {
                    content = r.ReadToEnd();
                }
                Match m = Regex.Match(content, String.Format(quickReminderProxyBindingCheck, Enum.GetName(typeof(Keys), m_InterOpKey), quickReminderProxyFile), RegexOptions.IgnoreCase | RegexOptions.Multiline);
                if (!m.Success)
                {
                    Trace.WriteLine("(Re)creating quickreminder proxy file");
                    bool wasReadOnly = false;
                    FileInfo f = new FileInfo(config);
                    if (f.IsReadOnly)
                    {
                        f.IsReadOnly = false;
                        wasReadOnly = true;
                    }
                    using (StreamWriter w = new StreamWriter(config))
                    {
                        w.Write(content);
                        if (!content.EndsWith(Environment.NewLine))
                            w.WriteLine();
                        w.Write(String.Format(quickReminderProxyBinding, Enum.GetName(typeof(Keys), m_InterOpKey), quickReminderProxyFile));
                    }
                    if (wasReadOnly)
                        f.IsReadOnly = wasReadOnly;
                }
                m_FullPathToInteropFile = Path.Combine(gamepath, quickReminderProxyFile);
            }
            catch (Exception ex)
            {
                Trace.WriteLine("Error checking InteropBinding: " + ex.Message);
            }
        }
    }
}
